package com.sodaopen.study.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sodaopen.study.entity.AjaxResult;
import com.sodaopen.study.entity.User;
import com.sodaopen.study.service.UserService;
import com.sodaopen.study.utils.ImageUtils;

@Controller
public class UserController {
	
	@Value("${web.upload-path}")
    private String filePath;

	@Autowired
    private UserService userService;


	@GetMapping("/profiles")
    public String profiles() {
    	return "user/profiles";
    }
    
    
    @PostMapping("/profiles")
    public String updateProfiles(User user, HttpSession session) {
    	User loginUser = (User)session.getAttribute("loginUser");
    	user.setId(loginUser.getId());
    	userService.updateProfiles(user);
    	
    	loginUser = userService.findUserByUsername(loginUser.getUsername());
    	session.setAttribute("loginUser", loginUser);
    	
    	return "user/profiles";
    }
    
    @GetMapping("/password")
    public String password() {
    	return "user/password";
    }
    
    
    @PostMapping("/password")
    public String updatePassword(String password, String newPassword, String rePassword, 
    		Model model, HttpSession session) {
    	
    	if(StringUtils.isEmpty(newPassword)
    			|| StringUtils.isEmpty(rePassword)
    			|| !newPassword.equals(rePassword)) {
    		
    		model.addAttribute("msg", "两次密码不一致！");
    		return "user/password";
    	}
    	
    	User loginUser = (User)session.getAttribute("loginUser");
    	if(!loginUser.getPassword().equals(password)) {
    		model.addAttribute("msg", "原密码错误！");
    		return "user/password";
    	}
    	
    	loginUser.setPassword(newPassword);
    	userService.updatePassword(loginUser);
    	model.addAttribute("msg", "密码更新成功！");
    	
    	return "user/password";
    }
    
    @GetMapping("/avatar")
    public String avatar() {
    	return "user/avatar";
    }
    
    @PostMapping("/avatar")
    @ResponseBody
    public Object updateAvatar(Integer x1, Integer y1, Integer x2, Integer y2, String imgPath, HttpSession session) {
    	AjaxResult result = new AjaxResult();
    	
    	File file = new File(filePath+imgPath);
    	
    	if(file.exists()) {
    		String suffixName = file.getName().substring(file.getName().lastIndexOf("."));
            String newName = UUID.randomUUID().toString() + suffixName;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            String mediaPath = "/u/" + sdf.format(new Date()) + "/" + newName;
            String path = filePath + mediaPath ;
    		try {
				ImageUtils.cutImage(file.getAbsolutePath(), path, x1, y1, x2, y2);
				
				User user = (User)session.getAttribute("loginUser");
				user.setPic(mediaPath);
				userService.updatePic(user);
				
				
				result.setSuccess(true);
			} catch (IOException e) {
				e.printStackTrace();
			}
    	}
    	
    	return result;
    }

}
